//
//  HomeTravelViewController.m
//  CivilianTravel
//
//  Created by 许仕杰 on 16/7/21.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "HomeTravelViewController.h"
#import "HomeTraverModel.h"
#import "HomeTravelTableViewCell.h"
#import "HomeTravelInformationViewController.h"
@interface HomeTravelViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
/** 数组 */
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation HomeTravelViewController

- (NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeTravelTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    [self requestTravelWithCityID:self.cityID];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HomeTraverModel *model = self.array[indexPath.row];
    [cell setCellWithModel:model];
    return cell;
}

- (void)requestTravelWithCityID:(NSString *)cityID{
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/city/gonglue" parameter:@{@"a_t":@"1469098975000",@"token":@"578624ce19a3f",@"eachpage":@5,@"cityid":cityID,@"sign":@"760adc22235a6b92c100e83ad40679ed35585717"} method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSSet *set = dic[@"data"];
        for (NSDictionary *smallDic in set) {
            HomeTraverModel *model = [[HomeTraverModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.array addObject:model];
        }
    } updateUI:^{
        [self.tableView reloadData];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 280;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTravelInformationViewController *travel = [[HomeTravelInformationViewController alloc] init];
    HomeTraverModel *model = self.array [indexPath.row];
    travel.yid = model.yid.description;
    travel.img = model.img;
    [self.navigationController pushViewController:travel animated:YES];
}





@end
